//导入远程组件
importJsx=[]

watchFileList=["pages"]













////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//远程静态资源
M.remoteStaticPath = "http://minglie.gitee.io/ming_api_mock_plugins/mi_react_quick_start_plus_fast";
let existsIndexHtml= fs.existsSync("./index.html")
const indexBaseUrl="http://minglie.gitee.io/ming_api_mock_plugins/mi_react_quick_start_plus_fast"
//下载默认indexHtml
if(!existsIndexHtml){
    M.mkdir("pages")
    M.get(indexBaseUrl+'/App.jsx').then(d=>{
        M.writeFile("./App.jsx",d);
    })
    M.get(indexBaseUrl+'/pages/MyCommont.jsx').then(d=>{
        M.writeFile("./pages/MyCommont.jsx",d);
    })
    M.get(indexBaseUrl+'/index.html').then(d=>{
        M.writeFile("./index.html",d);
    })
}

importJsxObj={}
importJsx.map(u=> {importJsxObj["/"+path.parse(u).base]=u})

console.log(importJsxObj)

app.begin(async (req,res)=>{
   // console.log(req.url)
   if(req.url.endsWith("jsx")||req.url.endsWith("js")){
         let fileContent=""
        if(importJsxObj[req.url]){
            fileContent=await M.getRemoteCacheByUrl(importJsxObj[req.url])
        }else{
            fileContent=M.readFile(staticPath+req.url)
        }
       fileContent= fileContent.replaceAll(/import .*/g,"").replaceAll(/export .*/g,"")
       res.renderJs(fileContent)
       return;
   }
   if(req.url.endsWith('.ico')){
       res.send("ok")
   }
})


function readFileTree(file,fileRealList){
     if(!file.children){
        fileRealList.push(file.value)
     }else{
        file.children.forEach(u=>{
            readFileTree(u,fileRealList)
        })
     }
}
insertImportJs="";

app.get("/index",(req,res)=>{
    
    if(!insertImportJs){
        fileList=M.getFileList(staticPath)
        // console.log(fileList)
            fileRealList=[]
            fileList.forEach(u => {
                readFileTree(u,fileRealList)
            });
            fileRealList=fileRealList.map(u=>u.replaceAll(staticPath,""))
            fileRealList=[...fileRealList,...Object.keys(importJsxObj)]
            fileList=fileRealList.filter(u=>(u.endsWith("jsx")||u.endsWith("js"))&& !u.endsWith("server.js")).sort().reverse().map(u=>`<script src="${u}" type="text/babel"></script>\n`)
      
            for(let i=0;i<fileList.length;i++){
                insertImportJs=insertImportJs+fileList[i];
            }
    }
    res.render("/index.html")
})



sseApp=M.sseServer()
app.get("/sseServer",sseApp)

M.watch=function (watch) {
    let t1=new Date().getTime();
    let t2=new Date().getTime();
    console.log(__dirname)
	console.log("watch on "+watch)
    fs.watch(watch, (event, file) => {
        if (file) {
           //console.log(event,"=======>event")
            if(event==="change"){
                 t2=new Date().getTime();
                if(t2-t1>300){
                     t1=t2;
                     console.log("change "+file)
                     sseApp.send("change")
                }
            }else if(event==="rename"){
                insertImportJs="";
                console.log("rename "+file)
                sseApp.send("rename")
            }
        }
    });
}


watchFileList=watchFileList.map(u=>staticPath+u)
watchFileList.push(staticPath)

watchFileList.forEach(u=>{
    M.watch(u)
})



